package com.lcredu.ajax.dao;

import com.lcredu.ajax.entity.User;
import com.lcredu.ajax.util.GetConnection;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @author 黎长容
 * @date 2025-07-03 20:00
 */
public class UserDao {
    /**
     * @param name
     * @return
     */
    public User getUserByName(String name) {
        User user = new User();
        try {
            Connection con = GetConnection.getCon();
            String sql = "select * from user where name = ?";
            PreparedStatement preparedStatement = con.prepareStatement(sql);
            preparedStatement.setString(1, name);
            ResultSet resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                int id = resultSet.getInt("id");
                String username = resultSet.getString("name");
                String password = resultSet.getString("password");
                String email = resultSet.getString("email");
                user.setName(username);
                user.setId(id);
                user.setPassword(password);
                user.setEmail(email);
                System.out.println(resultSet.getInt("id"));
                System.out.println(resultSet.getString("name"));
                System.out.println(resultSet.getString("password"));
                System.out.println(resultSet.getString("email"));

            }


        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return user;
    }
}
